/**
* @description: 待办工单 ==> 通信设备巡视 ==> 通信设备核查
**/
<template>
<view class="equipment-main">
  <nav-bar class="header" back title="设备巡视" @goBack="goBack"/>
  <view class="main-container">
    <!--基本信息-->
    <plateTitle title="基本信息" class="plate-title-1"/>
    <view class="base-info-container">
      <van-cell-group>
        <van-cell v-for="(item,index) in taskDetail.taskDetailArr" :key="index" :title="item.title" :value="item.text">
          <template #right-icon v-if="item.title === '实物ID'">
<!--            <span class="operation"><img src="../../../../static/img/scan.png" alt="" @click=""></span>-->
            <span class="operation" v-if="taskDetail.originData.entityid !== null" @click="scanClick">核对</span>
          </template>
        </van-cell>
      </van-cell-group>
    </view>
    
    <!--巡视信息-->
    <plateTitle title="巡视信息" class="plate-title-2"/>
    <view class="patrol-info-container">
      <van-field
        readonly
        clickable
        input-align="right"
        right-icon="arrow"
        :value="isCoincide"
        label="台账信息与现场信息是否一致"
        placeholder="点击选择"
        @click="showPicker = true"
      />
      <van-radio-group v-show="isCoincide === '不一致'"
                       v-model="operation"
                       icon-size="15px"
                       checked-color="#417ADC" style="padding: 10px 15px;">
        <van-radio name="noExist">现场无此设备，将标记此设备状态为“不存在”</van-radio>
        <van-divider dashed />
        <van-radio name="modify">现场设备信息不一致，将标记此设备状态为“修改”</van-radio>
      </van-radio-group>
    </view>
    
    <!--按钮-->
    <van-button block @click="confirmInfo">{{ buttonText() }}</van-button>
  
    <!--选择器-->
    <van-popup v-model="showPicker" position="bottom">
      <van-picker
        show-toolbar
        :columns="columns"
        @confirm="onConfirm"
        @cancel="showPicker = false"
      />
    </van-popup>
  </view>
</view>
</template>

<script>


let map_obj = {
  "name": "设备名称",   //设备名称
  "sitename": "所属站点",   //站点名称
  "parRoom": "所属机房",        //所属机房
  "parRack": "所属机柜编号",         //所属机柜编号
  "parRackScreen": "所属机柜屏眉",  //所属机柜屏眉
  "techType": "设备类型",  //设备类型
  "equlevel": "设备归属",   //设备归属
  "sysId": "所属传输系统", //所属传输系统
  "producerId": "设备品牌",   //设备品牌
  "modelnumber": "设备型号",    //设备型号
  "serilanum": "设备顺序",   //设备顺序
  "entityid": "实物ID",       //实物ID
  "begTim": "设备投运时间",    //设备投运时间
  "runState": "运行状态"
}

import navBar from "../../../../components/common/nav-bar";
import plateTitle from "../../../../components/business/plateTitle";
import { mapGetters } from "vuex";
import moment from "moment";
import wx from "../../../../static/common_js/jweixin_1.0.0";
import {Dialog, Toast} from "vant";
export default {
  components: { navBar, plateTitle },
  name: "equipmentPatrol",
  data() {
    return {
      objId: '', // 设备objid
      taskType: '', // 设备状态
      taskId: '', // 工单id
      buttonText: () => {
        if(this.isCoincide === "一致") return "保存";
        if(this.operation === "noExist") return "保存";
        if(this.operation === "modify") return "去完善台账信息";
      },
      isCoincide: "不一致",
      showPicker: false,
      columns: ["一致", "不一致"],
      operation: "modify",
      taskDetail: {}, // 巡视详情
      taskSiteName:'',
      taskSiteId: '', // 站点ID
      reTourCheck: '1', // 巡视-1 重新编辑-0
    }
  },
  computed: {
    ...mapGetters(['userData']),
  },
  onLoad (options) {
    this.objId = options.objId;
    this.taskType = options.state;
    this.taskId = options.taskId;
    this.taskSiteName = options.taskSiteName;
    this.taskSiteId = options.taskSiteId;
    this.getEquipmentInfo()
  },
  methods: {
    goBack() {
      uni.navigateTo({
        url: `/pages/workBench/todoTask/equipmentTour/equipmentTour?taskId=${this.taskId}&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
      })
    },
    getEquipmentInfo () {
      this.$api.todoTaskApi.equipemntDetail({objId: this.objId}).then(res => {
        this.taskDetail = {
          originData: res.data,
          taskDetailArr: Object.keys(map_obj).map(item => {
            return {
              title: map_obj[item],
              text: res.data[item]
            }
          })
        };
      })
    },
    onConfirm(value) {
      this.isCoincide = value;
      this.showPicker = false;
    },
    scanClick() {
      let _this = this;
      wx.scanQRCode({
        desc:"scanQRCode desc",
        needResult: 1,
        scanType:["qrCode"],
        success:function(res){
          let scanCodeId = res.resultStr
          _this.getCheckEntityE(scanCodeId)
        },
        error:function(res){
          console.log("scanQRCode error",res);
        }
      });
    },
    // 核对
    getCheckEntityE(scanCodeId) {
      let params = {
        objId: this.objId,
        entityId: scanCodeId
      }
      this.$api.getCheckEntity(params).then(res => {
        if(res.data.flag === true) {
          Dialog.confirm({
            message: '信息匹配成功',
          }).then(() => {
          })
        }else {
          Dialog.confirm({
            title:'信息不匹配',
            message: '该设备实物ID为：'+ res.data.entityId,
          }).then(() => {
          })
        }
      })
    },
    // 提交 一致/不一致
    confirmInfo () {
      // 选择一致或不存在时，保存提示
      if(this.isCoincide === "一致" || (this.isCoincide === "不一致" && this.operation === "noExist")) {
        Dialog.confirm({
          message: "所选巡视结果是否确认？确认后不可变更，如有问题请联系运维人员。",
        }).then(() => {
          
          if (this.isCoincide === "一致") {
            let checkSuccessParams = {
              "reTourCheck": this.reTourCheck, // 巡视-1 重新编辑-0
              "taskId": this.taskDetail.originData.taskId,
              "objId": this.objId,    //巡视设备ID    必填
              "tourOperId": this.userData.userId,    //巡视人ID    必填    userId放进来
              "tourOperName": this.userData.userName,    //巡视人名称    必填    userName放进来
              "tourTimStr":  moment().format('YYYY-MM-DD HH:mm:ss'),  //巡视时间 必填
            }
            this.$api.todoTaskApi.checkInfoSuccess(checkSuccessParams).then(res => {
              if(res.code === 0) {
                // 如果当前为巡视，且返回data为0，表示重复巡视
                if(this.reTourCheck === '1' && res.data === 0) {
                  Dialog.confirm({
                    message: "该设备已被巡视，请返回重试",
                  }).then(() => {
                    uni.navigateTo({
                      url: `/pages/workBench/todoTask/equipmentTour/equipmentTour?taskId=${ this.taskDetail.originData.taskId }&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
                    });
                  });
                } else {
                  Toast.success({
                    message: '保存成功',
                    onOpened: () => {
                      uni.navigateTo({
                        url: `/pages/workBench/todoTask/equipmentTour/equipmentTour?taskId=${ this.taskDetail.originData.taskId }&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
                      });
                    }
                  });
                }
              }
      
              else {
                Toast.fail("操作失败");
              }
            })
          }
          
          if (this.isCoincide === "不一致" && this.operation === "noExist") { // 认定设备不存在时进行保存
            let checkExistParams = {
              "reTourCheck": this.reTourCheck, // 巡视-1 重新编辑-0
              "taskId": this.taskDetail.originData.taskId,
              "objId": this.objId,    //巡视设备ID    必填
              "tourOperId": this.userData.userId,    //巡视人ID    必填    userId放进来
              "tourOperName": this.userData.userName,    //巡视人名称    必填    userName放进来
              "tourTimStr":  moment().format('YYYY-MM-DD HH:mm:ss'),  //巡视时间 必填
            }
            this.$api.todoTaskApi.checkInfoNotFound(checkExistParams).then(res => {
      
              if(res.code === 0) {
                // 如果当前为巡视，且返回data为0，表示重复巡视
                if(this.reTourCheck === '1' && res.data === 0) {
                  Dialog.confirm({
                    message: "该设备已被巡视，请返回重试",
                  }).then(() => {
                    uni.navigateTo({
                      url: `/pages/workBench/todoTask/equipmentTour/equipmentTour?taskId=${ this.taskDetail.originData.taskId }&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
                    });
                  });
                }
                else {
                  Toast.success({
                    message: '保存成功',
                    onOpened: () => {
                      uni.navigateTo({
                        url: `/pages/workBench/todoTask/equipmentTour/equipmentTour?taskId=${ this.taskDetail.originData.taskId }&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
                      });
                    }
                  });
                }
              }
      
              else {
                Toast.fail("操作失败");
              }
            })
          }
        }).catch(() => {})
      }


      if (this.isCoincide === "不一致" && this.operation === 'modify') { // 认定设备存在但是需要完善信息
        uni.navigateTo({
          url: `/pages/workBench/todoTask/equipmentTour/completeEquipmentInfo?type=update&objId=${this.objId}&taskId=${this.taskDetail.originData.taskId}&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
        })
      }
    }
  }
};
</script>

<style scoped lang="scss">
.equipment-main {
  .header {
    position: sticky;
    top: 0;
    z-index: 9;
  }
  .main-container {
    min-height: calc(100vh - #{px2vh(283)});
    padding: px2vh(30) px2vw(30) px2vh(90) px2vw(30);
    background-color: #F8F8F8;
    /deep/.van-field__label, 
    /deep/.van-field__control:read-only, 
    /deep/.van-field__body .van-field__control,
    /deep/.van-cell-group .van-cell
    {
      font-size: px2vw(42);
    }
    /deep/.van-field__label {
      width: auto;
    }
     
    .plate-title-1 {
      margin-top: px2vh(30);
      margin-bottom: px2vh(27);
    }
    
    .base-info-container {
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
  
      .operation {
        margin-left: px2vw(30);
        color: #417ADC;
        img {
          width: px2vw(42);
          vertical-align: text-top;
        }
      }
    }
    
    .plate-title-2 {
      margin-top: px2vh(60);
      margin-bottom: px2vh(27);
    }
    
    .patrol-info-container {
      font-size: px2vw(42);
      color: #666666;
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
  
      .van-radio-group {
        padding: px2vh(60) px2vw(42);
      }
      
      
    }
    
    /deep/.van-button {
      margin-top: px2vh(100);
      height: px2vh(120);
      color: #fff;
      background-color: #417ADC;
      border: 1px solid #417ADC;
      border-radius: px2vh(60);
    }
  }
}
</style>